package pw.hais.znyj.base.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.squareup.okhttp.Response;

import de.greenrobot.event.EventBus;
import pw.hais.http.base.OnHttpListener;
import pw.hais.utils.ApkInfoUtil;
import pw.hais.utils.EmptyUtil;
import pw.hais.utils.L;
import pw.hais.utils.SPUtil;
import pw.hais.znyj.base.dao.LeadDao;
import pw.hais.znyj.base.entity.EventT;
import pw.hais.znyj.base.model.CheckAPPModel;

/**
 * 检查更新
 * Created by Hais1992 on 2015/12/18.
 */
public class CheckUpdateService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        checkAppVersion();
    }


    /**
     * 更新检查
     */
    private void checkAppVersion() {
        LeadDao.checkAPPVersion(new OnHttpListener<CheckAPPModel>() {
            @Override
            public void onSuccess(Response response, CheckAPPModel data) {
                if (!ApkInfoUtil.getVersionName().equals(data.version) && !EmptyUtil.emptyOfString(data.version)) {
                    //发送广播
                    L.i("检查到新版本：" + ApkInfoUtil.getVersionName() + "---->" + data.version);
                    EventBus.getDefault().post(new EventT(EventT.EventType.UpdateApp, data));
                }
                SPUtil.saveObject("IS_SHOW_ADVERT", data.showAdvert);
            }
        });
    }
}
